<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>login</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="static/css/layui.css" media="all">
</head>
<body bgcolor="#faf5f5">

<ul class="layui-nav layui-bg-cyan">
    <li class="layui-nav-item"><a href="/sports">运动会管理系统</a></li>
</ul>


<div class="layui-container fly-marginTop" STYLE="background-color: white">
    <div class="fly-panel fly-panel-user" pad20="">
        <div class="layui-tab layui-tab-brief" lay-filter="user">
            <ul class="layui-tab-title">
                <li class="layui-this">找回密码</li>
            </ul>
            <div class="layui-form layui-tab-content" id="LAY_ucm" style="padding: 20px 0;">
                <div class="layui-tab-item layui-show">
                    <div class="layui-form layui-form-pane">
                        <form method="post" action="user/forget">


                            <div class="layui-form-item">
                                <label class="layui-form-label">学号</label>
                                <div class="layui-input-inline">
                                    <input type="text" id="L_loginName" name="loginName"
                                           required="" lay-verify="loginName"
                                           autocomplete="off" class="layui-input"></div>
                            </div>
                            <div class="layui-form-item"><label for="L_email" class="layui-form-label">邮箱</label>
                                <div class="layui-input-inline"><input type="text" id="L_email" name="email"
                                                                       required="" lay-verify="email"
                                                                       autocomplete="off" class="layui-input"></div>

                                <div class="layui-form-mid layui-word-aux">请输入预留的邮箱账号</div>
                            </div>
                            <div class="layui-form-item"><label for="L_code" class="layui-form-label">验证码</label>
                                <div class="layui-input-inline"><input type="text" id="L_code" name="code"
                                                                       required="" lay-verify="code"
                                                                       autocomplete="off" class="layui-input"></div>
                                <div class="layui-form-mid layui-word-aux">
                                    <input type="button" class="layui-btn layui-btn-primary layui-btn-sm" id="emailbtn"
                                           value="点击获取邮箱验证码"/>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <button class="layui-btn" lay-submit lay-filter="formDemo">确认重置</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="div1"></div>


<script th:src="@{static/layui.js}" charset="utf-8"></script>

<script>
    //Demo
    layui.use('form', function () {
        var form = layui.form;
        var $ = layui.jquery;

        //自定义验证规则
        form.verify({
            loginName: function (value) {
                if (value.length < 5) {
                    return '学号/工号至少得5个字符啊';
                }
            },
            email: [
                /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
                , '邮箱格式错误'
            ],
            code: [
                /^\d{6}$/
                , '验证码格式错误'
            ]
        });

        $(function () {
            $('#emailbtn').click(function () {
                let email_text = $('#L_email').val();
                if (email_text.match(/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/) == null) {
                    layer.msg("邮箱格式错误");
                } else {
                    $.post("email/forgetPwd", {email_text: email_text}, (res) => {
                        if (res == 'success') {
                            layer.msg("发送成功，请查收");
                            var count = 60;
                            var countdown = setInterval(CountDown, 1000);

                            function CountDown() {
                                $("#emailbtn").attr("disabled", true);
                                $("#emailbtn").val("请等待 " + count + " 秒后再试 !");
                                if (count == 0) {
                                    $("#emailbtn").val("重新获取").removeAttr("disabled");
                                    clearInterval(countdown);
                                }
                                count--;
                            }
                        } else {
                            layer.msg("发送失败");
                        }
                    })
                }

            })
        });

        //监听提交
        form.on('submit(formDemo)', function (data) {
            $.ajax({
                url: "user/forget",
                contentType: "application/json",
                type: 'POST',
                data: JSON.stringify(data.field),
                dataType: 'text',
                async: false,
                cache: false,
                success: function (result) {
                    if (result == "success") {
                        layer.msg('您的密码已重置为123456，请重新登陆', {
                            icon: 1,
                            time: 3000
                        }, function () {
                            window.location.href = "/sports/toPage?url=login";
                        });

                    } else {
                        layer.msg(result);
                    }
                },
                error: function (e) {
                    layer.msg("服务器好像出了问题，请稍后再试");
                }
            });
            return false;
        });
    });
</script>

</body>
</html>